Date.prototype.format = function(format){ 
    var o =  { 
    "M+" : this.getMonth()+1, //month 
    "d+" : this.getDate(), //day 
    "h+" : this.getHours(), //hour 
    "m+" : this.getMinutes(), //minute 
    "s+" : this.getSeconds(), //second 
    "q+" : Math.floor((this.getMonth()+3)/3), //quarter 
    "S" : this.getMilliseconds() //millisecond 
    };
    if(/(y+)/.test(format)){ 
    	format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
    }
    for(var k in o)  { 
	    if(new RegExp("("+ k +")").test(format)){ 
	    	format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
	    } 
    } 
    return format; 
};

function randomColor(){
	var r = Math.floor(Math.random()*256);
	var g = Math.floor(Math.random()*256);
	var b = Math.floor(Math.random()*256);
	return "rgb("+r+","+g+","+b+")";//IE7不支出rgb
}

function randomColor16(){
	//0-255	
	var r = random(255).toString(16);
	var g = random(255).toString(16);
	var b = random(255).toString(16);
	//255的数字转换成十六进制
	if(r.length<2)r = "0"+r;
	if(g.length<2)g = "0"+g;
	if(b.length<2)b = "0"+b;
	return "#"+r+g+b;
}

function random(num){
	return Math.floor(Math.random()*(num+1));
}

function randomRange(start,end){
	return Math.floor(Math.random()*(end-start+1))+start;
};


/**
 * 判断非空
 * 
 * @param val
 * @returns {Boolean}
 */
function isEmpty(val) {
	val = $.trim(val);
	if (val == null)
		return true;
	if (val == undefined || val == 'undefined')
		return true;
	if (val == "")
		return true;
	if (val.length == 0)
		return true;
	if (!/[^(^\s*)|(\s*$)]/.test(val))
		return true;
	return false;
}

function isNotEmpty(val) {
	return !isEmpty(val);
};


function getTimeFormat(startTime){
	var startTimeMills = startTime.getTime();
	var endTimeMills = new Date().getTime();
	var diff = parseInt((endTimeMills - startTimeMills)/1000);//秒
	var day_diff = parseInt(Math.floor(diff/86400));//天
	var buffer = Array();
	if(day_diff<0){
		return "[error],时间越界...";
	}else{
		if(day_diff==0 && diff<60){
			if(diff<=0)diff=1;
			buffer.push(diff+"秒前");
		}else if(day_diff==0 && diff<120){
			buffer.push("1 分钟前");
		}else if(day_diff==0 && diff<3600){
			buffer.push(Math.round(Math.floor(diff/60))+"分钟前");
		}else if(day_diff==0 && diff<7200){
			buffer.push("1小时前");
		}else if(day_diff==0 && diff<86400){
			buffer.push(Math.round(Math.floor(diff/3600))+"小时前");
		}else if(day_diff==1){
			buffer.push("1天前");
		}else if(day_diff<7){
			buffer.push(day_diff+"天前");
		}else if(day_diff <30){
			buffer.push(Math.round(Math.ceil( day_diff / 7 )) + " 星期前");
		}else if(day_diff >=30 && day_diff<=179 ){
			buffer.push(Math.round(Math.ceil( day_diff / 30 )) + "月前");
		}else if(day_diff >=180 && day_diff<365){
			buffer.push("半年前");
		}else if(day_diff>=365){
			buffer.push(Math.round(Math.ceil( day_diff /30/12))+"年前");
		}
	}
	return buffer.toString();
};

